package com.maguangwu.server.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maguangwu.dao.CheckGroupDao;
import com.maguangwu.entity.PageResult;
import com.maguangwu.entity.QueryPageBean;
import com.maguangwu.pojo.CheckGroup;
import com.maguangwu.server.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author mgw
 * @create 2021/10/31 9:56
 */
@Service("checkGroup")
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void addCkeckGroup(int[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.addCkeckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (int checkitemId : checkitemIds) {
                checkGroupDao.addCkeckGroupAndItem(checkGroupId, checkitemId);
            }
        }
    }

    @Override
    public PageResult<CheckGroup> checkGrouoFindPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = "";
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryString = "%" + queryPageBean.getQueryString() + "%";
        }
        Page<CheckGroup> checkGroups = checkGroupDao.checkGroupFindPage(queryString);
        PageResult<CheckGroup> pageResult = new PageResult<CheckGroup>(checkGroups.getTotal(), checkGroups);
        return pageResult;
    }
}
